package com.wise.site.protocol.result;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wise.site.protocol.base.SoapItem;

public class TabItem extends SoapItem{
	public int id;
	public String title;
	public int type;
	public int location;
	public List<IndexerBarEntry> indexBarList;
	public List<ShutcutEntry> shotcutList;
	
	public TabItem(JSONObject node){
		super(node);
	}

	@Override
	public void parse(JSONObject node) throws JSONException {
		id = node.getInt("id");
		title = node.getString("title");
		type = node.getInt("type");
		type = node.getInt("location");
		try{
			if(!node.isNull("menus")){
				indexBarList = new ArrayList<IndexerBarEntry>();
				JSONArray array = new JSONArray(node.getString("menus"));
				IndexerBarEntry entry = null;
				for(int i = 0; i < array.length(); i++){
					entry = new IndexerBarEntry(array.getJSONObject(i));
					indexBarList.add(entry);
				}
				
				Collections.sort(indexBarList, new Comparator<IndexerBarEntry>(){
					public int compare(IndexerBarEntry arg0, IndexerBarEntry arg1) {
						// TODO Auto-generated method stub
						return arg0.type - arg1.type;
					}
					
				});
			}
		}catch (Exception e) {
		}
		
		if(!node.isNull("shortcuts")){
			shotcutList = new ArrayList<ShutcutEntry>(); 
			JSONArray array = node.getJSONArray("shortcuts");
			
			ShutcutEntry entry = null;
			for(int i = 0; i < array.length(); i++){
				entry = new ShutcutEntry(array.getJSONObject(i));
				shotcutList.add(entry);
			}
		}
		
	}

}
